/*
 * Copyright (c) 2009, Manish Shakya,Real Time Solutions Pvt. Ltd.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *
 */
/**
*
*	\file	fio.c
*	\author Manish Shakya 
*	\brief
*	\date  Tuesday,October 27,2009
*	\version 1.0
*
*
*/
#include "peripherals.h"

#ifdef __FIO__

#define FIODIRX(X)		(*((volatile unsigned char *) (0x3FFFC000+(X)))) //byte access
#define FIOMASKX(X)		(*((volatile unsigned char *) (0x3FFFC010+(X)))) //byte access
#define FIOPINX(X) 		(*((volatile unsigned char *) (0x3FFFC014+(X)))) //byte access

/***********************************************************************************************************************/

void 
fio_init()
{
	SCS=1;

}
/***********************************************************************************************************************/

uint32_t 
fio_outport(uint32_t port,uint32_t bits,uint32_t values)
{
	ASSERT(port<=PORT3);
	FIOMASKX(port) =~bits;
	FIODIRX(port) = FIODIRX(port) | bits;
	FIOPINX(port) =values;
   return FIOPINX(port);
}

/***********************************************************************************************************************/

uint32_t 
fio_inport(uint32_t port,uint32_t bits)
{
	ASSERT(port<=PORT3);
	FIOMASKX(port) =~bits;
	FIODIRX(port) = FIODIRX(port) & (~bits);
	return FIOPINX(port) ;
}

/***********************************************************************************************************************/

uint32_t 
fio_toggle(uint32_t port,uint32_t bits)
{
	ASSERT(port<=PORT3);
	
	FIOMASKX(port) =~bits;
	FIODIRX(port) = FIODIRX(port) | bits;
	FIOPINX(port)=FIOPINX(port)^0xFF ;
   return FIOPINX(port);
}

/***********************************************************************************************************************/

uint32_t 
fio_outport32(uint32_t bits,uint32_t values)
{
	//ASSERT(port<=PORT3);
	FIOMASK =~bits;
	FIODIR = FIODIR | bits;
	FIOPIN = values;
   return FIOPIN;
}

/***********************************************************************************************************************/

uint32_t 
fio_inport32(uint32_t bits)
{
	//ASSERT(port<=PORT3);
	FIOMASK =~bits;
	FIODIR = FIODIR & (~bits);
	return FIOPIN ;
}

/***********************************************************************************************************************/

uint32_t 
fio_toggle32(uint32_t bits)
{
	//ASSERT(port<=PORT3);
	
	FIOMASK =~bits;
	FIODIR = FIODIR | bits;
	FIOPIN=FIOPIN^0xFFFFFFFF ;
   return FIOPIN;
}

/***********************************************************************************************************************/
#if 0
uint32_t 
fio_outport0(uint32_t bits,uint32_t values)
{
	
	FIOMASKX0 &=~bits;
	FIODIRX0 = FIODIRX0 | bits;
	FIOPINX0 =values;
   return FIOPINX0;
}

/***********************************************************************************************************************/
uint32_t 
fio_outport1(uint32_t bits,uint32_t values)
{
	
	FIOMASKX1 &=~bits;
	FIODIRX1 = FIODIRX2 | bits;
	FIOPINX1 =values;
	return FIOPINX1;

}

/***********************************************************************************************************************/
uint32_t 
fio_outport2(uint32_t bits,uint32_t values)
{
	
	FIOMASKX2 &=~bits;
	FIODIRX2 = FIODIRX2 | bits;
	FIOPINX2 =values;
	return 	FIOPINX2;

}

/***********************************************************************************************************************/

uint32_t 
fio_outport3(uint32_t bits,uint32_t values)
{
	
	FIOMASKX3 &=~bits;
	FIODIRX3 = FIODIRX3 | bits;
	FIOPINX3 =values;
	return FIOPINX3;

}

/***********************************************************************************************************************/

uint32_t 
fio_inport0(uint32_t bits)
{
	FIOMASKX0 &=~bits;
	FIODIRX0 = FIODIRX0 & (~bits);
	return FIOPINX0 ;
}
/***********************************************************************************************************************/

uint32_t 
fio_inport1(uint32_t bits)
{
	FIOMASKX1 &=~bits;
	FIODIRX1 = FIODIRX1 & (~bits);
	return FIOPINX1 ;
}

/***********************************************************************************************************************/

uint32_t 
fio_inport2(uint32_t bits)
{
	FIOMASKX2 &=~bits;
	FIODIRX2 = FIODIRX2 & (~bits);
	return FIOPINX2 ;
}
/***********************************************************************************************************************/

uint32_t 
fio_inport3(uint32_t bits)
{
	FIOMASKX3 &=~bits;
	FIODIRX3 = FIODIRX3 & (~bits);
	return FIOPINX3 ;
}
#endif
#endif






